package com.kotlin.myapplication.mvp.view;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.RadioButton;

import com.kotlin.myapplication.R;
import com.kotlin.myapplication.utils.BitmapUtil;


/**
 * 居中显示文字和图片的radioButton,支持上下左右方向的图片
 * @author zhazb
 *
 */
@SuppressLint("AppCompatCustomView")
public class CenterRadioButton extends RadioButton {
	
	/**
	 * 居中方式
	 */
	private int centerType = 0;


    public CenterRadioButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public CenterRadioButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
    	TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CenterRadioButton);
    	centerType = typedArray.getInt(R.styleable.CenterRadioButton_center_direction, 0);
    	typedArray.recycle();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    	switch (centerType) {
		case 0: //上
			topCenter();
			break;
		case 1://左
			leftCenter();
			break;
		case 2://下
			bottomCenter();
			break;
		case 3://右
			rightCenter();
			break;

		default:
			break;
		}
    }
    
    private void topCenter() {
        Drawable[] compoundDrawable = getCompoundDrawables();
        Drawable topDrawable = compoundDrawable[1];
        if (topDrawable == null) {
        	return;
        }
        Bitmap topBitmap = BitmapUtil.drawableToBitmap(topDrawable);
        int topHeight = topBitmap.getHeight();
        Paint paint = getPaint();
        String content = getText().toString();
        Rect bounds = new Rect();
        paint.getTextBounds(content, 0, content.length(), bounds);
        int contentHeight = TextUtils.isEmpty(content) ? 0 : bounds.height();
        int padding = TextUtils.isEmpty(content) ? 0 : getCompoundDrawablePadding();
        int widgetHeight = getHeight();
        int offset = Math.abs((widgetHeight - topHeight - contentHeight)) >> 1;
        setPadding(0, offset - padding, 0, 0);
    }
    
    private void leftCenter() {
        Drawable[] compoundDrawable = getCompoundDrawables();
        Drawable leftDrawable = compoundDrawable[0];
        if (leftDrawable == null) {
        	return;
        }
        Bitmap leftBitmap = BitmapUtil.drawableToBitmap(leftDrawable);
        int leftWidth = leftBitmap.getWidth();
        Paint paint = getPaint();
        String content = getText().toString();
        Rect bounds = new Rect();
        paint.getTextBounds(content, 0, content.length(), bounds);
        int contentWidth = TextUtils.isEmpty(content) ? 0 : bounds.height();
        int widgetWidth = getWidth();
        int padding = TextUtils.isEmpty(content) ? 0 : getCompoundDrawablePadding();
        int offset = Math.abs((widgetWidth - leftWidth - contentWidth)) >> 1;
        setPadding(offset - padding, 0, 0, 0);
    }
    
    private void bottomCenter() {
        Drawable[] compoundDrawable = getCompoundDrawables();
        Drawable bottomDrawable = compoundDrawable[3];
        if (bottomDrawable == null) {
        	return;
        }
        Bitmap bottomBitmap = BitmapUtil.drawableToBitmap(bottomDrawable);
        int bottomHeight = bottomBitmap.getHeight();
        Paint paint = getPaint();
        String content = getText().toString();
        Rect bounds = new Rect();
        paint.getTextBounds(content, 0, content.length(), bounds);
        int contentHeight = TextUtils.isEmpty(content) ? 0 : bounds.height();
        int widgetHeight = getHeight();
        int padding = TextUtils.isEmpty(content) ? 0 : getCompoundDrawablePadding();
        int offset = (widgetHeight - bottomHeight - contentHeight) >> 1;
        setPadding(0, 0, 0, offset - padding);
    }
    
    private void rightCenter() {
        Drawable[] compoundDrawable = getCompoundDrawables();
        Drawable rightDrawable = compoundDrawable[2];
        if (rightDrawable == null) {
        	return;
        }
        Bitmap rightBitmap = BitmapUtil.drawableToBitmap(rightDrawable);
        int rightWidth = rightBitmap.getWidth();
        Paint paint = getPaint();
        String content = getText().toString();
        Rect bounds = new Rect();
        paint.getTextBounds(content, 0, content.length(), bounds);
        int contentWidth = TextUtils.isEmpty(content) ? 0 : bounds.height();
        int widgetWidth = getWidth();
        int padding = TextUtils.isEmpty(content) ? 0 : getCompoundDrawablePadding();
        int offset = (widgetWidth - rightWidth - contentWidth) >> 1;
        setPadding(0, 0, offset - padding, 0);
    }
}
